package com.htx.oldbookpavilion.web.home;

import com.htx.oldbookpavilion.common.StandardResponse;
import com.htx.oldbookpavilion.entity.ShoppingList;
import com.htx.oldbookpavilion.service.homeservice.BookDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/manager")
public class BookDetailController {

    @Autowired
    private BookDetailService bookDetailService;

    /**
     * 添加书籍到购书清单
     * @param shoppingList
     * @return
     */
    @RequestMapping("/addToShoppingList")
    public StandardResponse addToShoppingList(@RequestBody ShoppingList shoppingList) {
        StandardResponse<Boolean> sResponse = new StandardResponse<>();
        if (shoppingList.getBookUserId().longValue() == shoppingList.getUserInfoId().longValue()) {
            sResponse.setSuccess(false);
            sResponse.setMsg("不能添加自己的书到购书清单");
            sResponse.setData(null);
            return sResponse;
        }
        boolean isAdded = bookDetailService.isAddToShoppingList(shoppingList);
        if (!isAdded) {
            sResponse.setSuccess(false);
            sResponse.setMsg("添加失败，该书籍已经添加到购书清单");
            sResponse.setData(null);
            return sResponse;
        }
        boolean success = bookDetailService.addToShoppingList(shoppingList);
        if (success) {
            sResponse.setSuccess(true);
            sResponse.setMsg("添加成功");
            sResponse.setData(true);
        } else {
            sResponse.setSuccess(false);
            sResponse.setMsg("添加失败");
            sResponse.setData(null);
        }
        return sResponse;
    }

    /**
     * 校验此书是否已经添加到购书清单
     * @param shoppingList
     * @return
     */
    @RequestMapping("/isAddToShoppingList")
    public StandardResponse isAddToShoppingList(ShoppingList shoppingList) {
        StandardResponse<Boolean> sResponse = new StandardResponse<>();
        if (shoppingList.getUserInfoId() == null || shoppingList.getBookId() == null) {
            sResponse.setSuccess(false);
            sResponse.setMsg("参数传递错误");
            sResponse.setData(null);
            return sResponse;
        }
        boolean isAdded = bookDetailService.isAddToShoppingList(shoppingList);
        sResponse.setSuccess(true);
        if (isAdded){
            sResponse.setMsg("未添加到购书清单");
            sResponse.setData(true);
        }else{
            sResponse.setMsg("已添加到购书清单");
            sResponse.setData(false);
        }
        return sResponse;
    }
}
